<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Brank;
use App\Models\GrUser;
use App\Models\GrUserBank;
use App\Models\UserPay;
use App\Pay\BaiduYunMessage;
use App\Pay\FullDiscountStrategy;
use App\Pay\Pay;
use App\service\FullReductionStrategy;
use App\Service\GetCode;
use App\service\JwtService;
use App\service\ReturnService;
use App\Service\SendCode;
use App\service\SendMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;

class BrankController extends Controller
{
    /**
     * 微信授权登录
     */
    public function submitLogin(Request $request)
    {

        $strategy1 = new \App\service\Pay(new FullReductionStrategy());
        $strategy1->getDiscount(1000);

        //获取前段信息
        $data = $request->all();
        $nickName = $data['nickName'];
        $code = $data['code'];
        $avatar_url = $data['avatarUrl'];
        $appid = "wxc8a947ddc01ffbc4";
        $secret = "7efa9bd82678e164067e6c3a94bc7c7c";
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $urlData = json_decode(file_get_contents($url), true);
        $session_key = $urlData['session_key'];
        $openid = $urlData['openid'];
        $userRes = GrUser::where('openid', $openid)->first();
        $a[] = ['openid' => $openid, 'session_key' => $session_key, 'nick_name' => $nickName, 'avatar_url' => $avatar_url];
        $token = JwtService::getToken($userRes['id']);
        if (empty($userRes)) {
            $res = GrUser::insert($a);
            if ($res) {
                return ['code' => 200, 'message' => '成功'];
            }
        } else {
            return ['code' => 200, 'message' => '成功', 'data' => $token];
        }
    }


    /**
     * 判断支付密码是否正确
     */
    public function isPayPassword(Request $request)
    {
        //获取前段数据
        $pay_password = $request->post('pay_password');
        $token = $request->post('token');
        $jwtData = JwtService::decodeToken($token);
        $user_id = $jwtData->data;
        $data = Admin::where('id', $user_id)->first();
        if ($data['payment_code'] != md5($pay_password)) {
            return ReturnService::failedReturn('400', '密码错误');
        } else {
            return ReturnService::successReturn('200', '密码正确', []);
        }
    }

    /**
     * 获取个人信息
     */
    public function personalInformation(Request $request)
    {
        //获取前段数据
        $token = $request->post('token');
        $jwtData = JwtService::decodeToken($token);
        $user_id = $jwtData->data;
        $data = Admin::where('id', $user_id)->first();
        return ['code' => 200, 'message' => '成功', 'data' => ['real_name' => $data['real_name'], 'id_card' => $data['id_card'],
            'balance' => $data['balance']]];
    }

    /**
     * 绑定银行卡
     */
    public function bindBankCar(Request $request)
    {
        $bankCar = $request->post('bank_number');
        $tel = $request->post('tel');
        $bank = Brank::where('bank_number', $bankCar)->first();
        if (empty($bank)) {
            return ReturnService::failedReturn('400', '银行卡错误');
        }
        $tel = Brank::where('tel', $tel)->first();
        if (empty($tel)) {
            return ReturnService::failedReturn('401', '手机号错误');
        }
        return ReturnService::successReturn('200', '成功', $tel);
    }


    /**
     * 生成验证码
     */
    public function getCode(Request $request)
    {
        //获取前台数据
        $tel = $request->get('tel');
        //生成验证码
        $code = GetCode::getCode(1000, 9999);
        //获取验证码
        //$res=SendMessage::SendCode($code,$tel);
        if ($code) {
            Cache::put('code' . $tel, $code);
            return ['code' => 200, 'message' => '发送成功', 'data' => $code];
        }
    }

    /**
     * 判断验证码是否正确
     */
    public function isCode(Request $request)
    {
        $token = $request->post('token');
        $jwtData = JwtService::decodeToken($token);
        $user_id = $jwtData->data;
        //获取数据
        $bank_number = $request->post('bank_number');
        $tel = $request->post('tel');
        $code = $request->post('code');
        $captcha = Cache::get('code' . $tel);
        if ($code != $captcha) {
            return ['code' => 402, 'message' => '验证码错误'];
        } else {
            $data = GrUserBank::where('bank_number', $bank_number)->where('tel', $tel)->where('user_id', $user_id)->first();
            if (empty($data)) {
                $res = GrUserBank::insert(['bank_number' => $bank_number, 'tel' => $tel, 'user_id' => $user_id]);
                if ($res) {
                    return ReturnService::successReturn('200', '添加成功', $bank_number);
                } else {
                    return ReturnService::failedReturn('400', '添加失败');
                }
            } else {
                return ReturnService::successReturn('201', '你已经绑定本银行卡', $bank_number);
            }

        }
    }

    /**
     * 消费记录
     */
    public function payRecode(Request $request)
    {
        $token = $request->post('token');
        $jwtData = JwtService::decodeToken($token);
        $user_id = $jwtData->data;
        $validator = Validator::make(['user_id', $user_id], [
            'user_id' => 'required|numeric'
        ]);
        if ($validator->fails()) {
            echo "只能是数字";
        }
        $time = strtotime($request->post('date'));
        $start = date('Y-m-01 ', $time);//获取指定月份的第一天
        $end = date('Y-m-t', $time); //获取指定月份的最后一天
        //指出
        $payOut = UserPay::where('pay_time', '>', $start)->where('pay_time', '<', $end)->where('pay_status', 0)->where('user_id', $user_id)->sum('pay_money');
        //收入
        $payIN = UserPay::where('pay_time', '>', $start)->where('pay_time', '<', $end)->where('pay_status', 1)->where('user_id', $user_id)->sum('pay_money');
        $data = UserPay::where('pay_time', '>', $start)->where('pay_time', '<', $end)->where('user_id', $user_id)->get()->toArray();
        return ['code' => 200, 'message' => '成功', 'data' => ['payOut' => $payOut, 'payIn' => $payIN, 'data' => $data]];


    }

    /**
     * 支付明细
     */
    public function payDetail(Request $request)
    {
        $time = $request->post('date');
        $token = $request->post('token');
        $jwtData = JwtService::decodeToken($token);
        $user_id = $jwtData->data;
        if ($time == null) {
            $time = time();
            $start = date('Y-m-01 ', $time);//获取指定月份的第一天
            $end = date('Y-m-t', $time); //获取指定月份的最后一天
            //支出
            $payOut = UserPay::where('pay_time', '>', $start)->where('pay_time', '<', $end)->where('pay_status', 0)->where('user_id', $user_id)->sum('pay_money');
            //收入
            $payIN = UserPay::where('pay_time', '>', $start)->where('pay_time', '<', $end)->where('pay_status', 1)->where('user_id', $user_id)->sum('pay_money');
            $data = UserPay::where('pay_time', '>', $start)->where('pay_time', '<', $end)->where('user_id', $user_id)->get()->toArray();
            return ['code' => 200, 'message' => '成功', 'data' => ['payOut' => $payOut, 'payIn' => $payIN, 'data' => $data]];
        }

    }

    /**
     * 获取绑定的银行卡
     */
    public function topUp(Request $request)
    {
        $token = $request->post('token');
        $jwtData = JwtService::decodeToken($token);
        $user_id = $jwtData->data;
        $data = GrUserBank::join('gr_brank', 'gr_user_bank.bank_number', '=', 'gr_brank.bank_number')
            ->where('gr_user_bank.user_id', $user_id)->get()->toArray();
        if ($data) {
            return ReturnService::successReturn('200', '成功', $data);
        } else {
            return ReturnService::failedReturn('400', '失败');
        }


    }

    /**
     * 充值
     */
    public function skype()
    {

    }
}
